# -*- coding: utf-8 -*-
from odoo import models, api, fields, tools
from odoo.exceptions import UserError
import re


class township_village(models.Model):

    _name = "proman.township_village"
    _rec_name = "complete_name"
    _description = "项目区定义"
    _table = "proman_township_village"
    _parent_name = "parent_id"
    _parent_store = True
    _order = "sequence"
    _inherit =  ['mail.thread', 'mail.activity.mixin', 'image.mixin']

    sequence = fields.Integer(string="排序",translate = 0)
    name = fields.Char(string="项目区定义名称",translate = 0)
    code = fields.Char(string="区域编码",translate = 0)
    parent_id = fields.Many2one(string="归属上级",comodel_name = 'proman.township_village',translate = 0)
    region_id = fields.Many2one(string="省市县区",comodel_name = 'setcent.region',translate = 0)
    child_id = fields.One2many(string="子类别",comodel_name = 'proman.township_village',inverse_name = 'parent_id',readonly = 1,translate = 0)
    complete_name = fields.Char(string="完整路径名",compute = '_compute_complete_name',store = 1,translate = 0)
    parent_path = fields.Char(string="父路径",index = 1,translate = 0)
    level = fields.Integer(string="层级",compute = '_compute_level',store = 1,translate = 0)
    address = fields.Char(string="具体地址",translate = 0)
    logitude = fields.Float(string="经度",translate = 0)
    latitude = fields.Float(string="纬度",translate = 0)
    manage_department_id = fields.Many2one(string="管理机构",comodel_name = 'hr.department',default = lambda self: self.env.user.default_department,translate = 0)
    image = fields.Binary(string="照片1",translate = 0)
    image1 = fields.Binary(string="相关照片2",translate = 0)
    state = fields.Selection(string="状态",selection = [('1', '草稿'), ('2', '提交'), ('3', '审核')],default='1',translate = 0)
    base_information = fields.Html(string="行政区划人口概况",translate = 0)
    natural_resources = fields.Html(string="自然资源概况",translate = 0)
    social_economy = fields.Html(string="社会经济概况",translate = 0)
    detail_contents = fields.Html(string="其它情况",translate = 0)
    attachment = fields.Many2many(string="相关附件",comodel_name = 'ir.attachment',translate = 0)
    _sql_constraints =  [
        ('name_uniq', 'unique (parent_id,name)', '同类别下名称相同'),       ]

    # 获取 归属上级 的 省市县区
    @api.onchange('parent_id')
    def _onchange_parent_id(self):
        for record in self:
            record.region_id = record.parent_id.region_id

    def button1(self):
        if self.state == '1':
            self.write({'state': '2'})
        # 审核
    def button2(self):
        if self.state == '2':
            self.write({'state': '3'})
        # 退回
    def button3(self):
        if self.state == '2':
            self.write({'state': '1'})

        # 反审核
    def button4(self):
        if self.state == '3':
            self.write({'state': '2'})
    @api.depends('name', 'parent_id.complete_name')
    def _compute_complete_name(self):
        for category in self:
            if category.parent_id:
                category.complete_name = '%s / %s' % (category.parent_id.complete_name, category.name)
            else:
                category.complete_name = category.name
    @api.depends("parent_path")
    def _compute_level(self):
        for rc in self:
            if rc.parent_path:
                rc.level = str(rc.parent_path).count("/")
            else:
                rc.level = 0

    def unlink(self):
        for record in self:
            if record.state != '1':
                raise UserError("非草稿数据，不可删除，请取消并检查！")
        res = super(township_village, self).unlink()
        return res
    def name_get(self):
        if not self.env.context.get('hierarchical_naming', True):
            return [(record.id, record.name) for record in self]
        return super(township_village, self).name_get()

